<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/4
 * Time: 9:24
 */

namespace Api\Controller;
use Admin\Model\NewsModel;
use Admin\Model\NewsCateModel;

class NewsController extends ApiController
{
    private $news;
    private $newsCate;
    function __construct() {
        parent::__construct();
        $this->news =  new NewsModel();
        $this->newsCate = new NewsCateModel();
    }
    /**
     * @todo 根据新闻type 获取 新闻列表  0 普通 1 头条  2 热门
     * @param int $type
     * @param int $limit  获取条数
     */
    public function getNewsListByType($type=1,$limit=5){
        $type = I('type',$type);
        $limit = I('limit',$limit);
        $where = array(
            'type'=>$type
        );
        $data =  $this->news->getNewsList($where,$limit);
        return $this->callBack("新闻列表",$this->SUCCESS_CODE,$data);
    }
    
    
    /**
     * @todo 根据新闻分类flag 标签 获取新闻列表 和新闻分类 信息
     * @param string $flag 新闻分类标签   xyxw,zfgl
     * @param int $limit    
     * @param int $type 
     */
    public function getNewsListByFlag($flag,$limit=5,$type=''){
        $flag = I('flag',$flag);
        $type = I('type',$type);
        $data['cateInfo'] = $this->newsCate->getCateInfoByFlag($flag);
        
        $subCate = $this->newsCate->selectSubCatesByFlag($flag);
        $subIds = [$data['cateInfo']['id']];
        foreach($subCate['subCates'] as $v){
            $subIds[] = $v['id'];
        }
        $where = array(
            'cate_id'=>array('in',$subIds)
        );
        if(!empty($type)){
            $where['type'] = array('eq',$type);
        }
        $data['newsList'] = $this->news->getNewsList($where,$limit);
        
        return $this->callBack("新闻列表",$this->SUCCESS_CODE,$data);
        
    }
    
    /**
     * @todo 根据多个新闻分类flag标签 获取新闻列表 和新闻分类 信息
     * @param string $flag 新闻分类标签   xyxw,zfgl
     * @param int $limit
     * @param int $type 类型
     */
    public function getNewsListByFlags($flag,$limit=5,$type){
        $flag = I('flag',$flag);
        $type = I('type',$type);
        $cateInfo = $this->newsCate->getCateInfoByFlags($flag);
        foreach ($cateInfo as $v){
            $cates[] = $v['id'];
        }
        $where = array(
            'cate_id'=>array('in',$cates)
        );
        if(!empty($type)){
            $where['type'] = array('eq',$type);
        }
        $newsList = $this->news->getNewsList($where,$limit);
        
        return $this->callBack("新闻列表",$this->SUCCESS_CODE,$newsList);
    }
    
    /**
     * @todo 根据新闻id获取新闻内容
     * @param type $id
     * @return type
     */
    public function getNewsInfoById($newsId){
        $newsId = I('newsId',$newsId);
        if(empty($newsId)){
            return $this->callBack("未知错误",$this->ERROR_CODE);
        }
        $data['newsInfo'] = $this->news->getNewsInfoById($newsId);
        $data['newsCate'] = $this->newsCate->getCateInfoById($data['newsInfo']['cate_id']);
        return $this->callBack("新闻内容",$this->SUCCESS_CODE,$data);
    }
    
    /**
     * @todo 根据新闻id 和新闻所在分类 获取 上一条新闻内容
     * @param type $id
     * @param type $cate_id
     */
    public function getNewsPreById($id,$cate_id){
        $newsId = I('newsId',$id);
        $cateId = I('cateId',$cate_id);
        
        $data = $this->news->getNewsPreById($newsId,$cateId);
        return $this->callBack("新闻内容",$this->SUCCESS_CODE,$data);
    }
    /**
     * @todo 根据新闻id 和新闻所在分类 获取 下一条新闻内容
     * @param type $id
     * @param type $cate_id
     */
    public function getNewsNextById($id,$cate_id){
        $newsId = I('newsId',$id);
        $cateId = I('cateId',$cate_id);
        $data = $this->news->getNewsNextById($newsId,$cateId);
        return $this->callBack("新闻内容",$this->SUCCESS_CODE,$data);
    }
    
    
    public function getNewsSubCatesByFlag($flag){
        $flag = I('flag',$flag);
        $data = $this->newsCate->selectSubCatesByFlag($flag);
        return $this->callBack("新闻内容",$this->SUCCESS_CODE,$data);
    }

}